package com.hrserviceserve.service;

import com.hrserviceserve.dao.ContractRepository;
import com.hrserviceserve.dao.SalaryRepository;
import com.hrserviceserve.entity.Contract;
import com.hrserviceserve.entity.Result;
import com.hrserviceserve.entity.Salary;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.transaction.Transactional;
import java.util.List;
import java.util.Map;

@Service
public class SalaryService {//工资
    @Autowired
    SalaryRepository salaryRepository;
    @Autowired
    private EntityManager entityManager;

    public Result findByPage(Map<String,Object> map){

            PageRequest pageRequest=PageRequest.of((int)map.get("page")-1,(int)map.get("limit"));
            Page<Salary> page=salaryRepository.findAllWhere((Integer) map.get("personnelId"),pageRequest);
            System.out.println(page.getContent());
            return Result.success(page);
    }


    public Result save(Salary salary){
        Salary save = salaryRepository.save(salary);
        if (save!=null){
            return Result.success(save);
        }else {
            return Result.fail();
        }
    }
    @Transactional
    @Modifying(clearAutomatically = true, flushAutomatically = true)
    public int delete(Integer id){
        return salaryRepository.deleteByPersonnelId(id);
    }
}
